<?php

class Admin_Form_ValidateProduct {

  // CHUA NHUNG THONG BAO LOI CUA FORM
  protected $_messagesError = null;

  // MANG CHUA DU LIEU SAU KHI KIEM TRA
  protected $_arrData;

  public function __construct($arrParam = array(), $options = null) {

    if ($arrParam['action'] == 'add') {
      $options = array('table' => 'products', 'field' => 'name');
    } else if ($arrParam['action'] == 'edit') {
      $clause = ' id !=' . $arrParam['id'];
      $options = array('table' => 'products', 'field' => 'name', 'exclude' => $clause);
    }

    // ========================================
    // KIEM TRA product_name
    // ========================================
    
    $validator = new Zend_Validate();
    $validator->addValidator(new Zend_Validate_NotEmpty(), true)->addValidator(new Zend_Validate_StringLength(2, 32), true)
        ->addValidator(new Zend_Validate_Db_NoRecordExists($options), true);
    if (!$validator->isValid($arrParam['name'])) {
      $message = $validator->getMessages();
      $this->_messagesError['name'] = 'Product name: ' . current($message);
      $arrParam['name'] = '';
    }
    // ========================================
    // KIEM TRA Picture
    // ========================================
    /*$upload = new Zend_File_Transfer_Adapter_Http();
    $fileInfo = $upload->getFileInfo('picture');
    $fileName = $fileInfo['picture']['name'];

    $upload->addValidator('Extension', true, array('jpg', 'gif', 'png'), 'picture');
    $upload->addValidator('Size', true, array('min' => '2KB', 'max' => '1000KB'), 'picture');
    if (!$upload->isValid('picture') && empty($fileName)) {
      $message = $upload->getMessages();
      $this->_messagesError['picture'] = 'Picture: ' . current($message);
    }
    */

    // ========================================
    // KIEM TRA Price
    // ========================================
    $validator = new Zend_Validate();
    $validator->addValidator(new Zend_Validate_NotEmpty(), true)
              ->addValidator(new Zend_Validate_StringLength(2, 32), true);
    if (!$validator->isValid($arrParam['price'])) {
      $message = $validator->getMessages();
      $this->_messagesError['price'] = 'Price: ' . current($message);
      $arrParam['price'] = '';
    }

    // ========================================
    // KIEM TRA Order
    // ========================================
    $validator = new Zend_Validate();
    $validator->addValidator(new Zend_Validate_NotEmpty(), true)->addValidator(new Zend_Validate_Int(), true);
    if (!$validator->isValid($arrParam['order'])) {
      $message = $validator->getMessages();
      $this->_messagesError['order'] = 'Order: ' . current($message);
      $arrParam['order'] = '';
    }
    
    // ========================================
    // KIEM TRA Special
    // ========================================
    if (empty($arrParam['special']) || !isset($arrParam['special'])) {
      $arrParam['special'] = 0;
    }

    // ========================================
    // KIEM TRA status
    // ========================================
    if (empty($arrParam['status']) || !isset($arrParam['status'])) {
      $arrParam['status'] = 0;
    }
    
    // ========================================
    // KIEM TRA Content
    // ========================================
    $validator = new Zend_Validate();
    $validator->addValidator(new Zend_Validate_NotEmpty(), true);
    if (!$validator->isValid($arrParam['content'])) {
      $message = $validator->getMessages();
      $this->_messagesError['content'] = 'Content: ' . current($message);
      $arrParam['content'] = '';
    }
    $this->_arrData = $arrParam;
  }

  // Kiem tra Error
  // return true neu co loi xuat hien
  public function isError() {
    if (count($this->_messagesError) > 0) {
      return true;
    } else {
      return false;
    }
  }

  // Tra ve mot mang cac loi
  public function getMessageError() {
    return $this->_messagesError;
  }

  // Tra ve mot mang du lieu sau khi kiem tra
  public function getData($options = null) {
    if ($options['upload'] == true) {
      $this->_arrData['picture'] = $this->uploadFile();
    }
    return $this->_arrData;
  }

  public function uploadFile() {
    // Duong dan den thu muc upload
    $upload_dir = UPLOADS_PATH . '/products';
    // ========================================
    // UPLOAD FILE $user_avatar
    // ========================================
    $upload = new Zendvn_File_Upload();
    $fileInfo = $upload->getFileInfo('picture');
    $fileName = $fileInfo['picture']['name'];
    if (!empty($fileName)) {
      $fileName = $upload->upload('picture', $upload_dir . '/orignal', array('task' => 'rename'), 'product_');

      $thumb = Zendvn_File_Images::create($upload_dir . '/orignal/' . $fileName);
      $thumb->resize(150, 176)->save($upload_dir . '/img150x176/' . $fileName);

      $thumb = Zendvn_File_Images::create($upload_dir . '/orignal/' . $fileName);
      $thumb->resize(226, 310)->save($upload_dir . '/img226x310/' . $fileName);
      
      $thumb = Zendvn_File_Images::create($upload_dir . '/orignal/' . $fileName);
      $thumb->resize(80, 75)->save($upload_dir . '/img80x75/' . $fileName);

      if ($this->_arrData['action'] == 'edit') {
        $upload->removeFile($upload_dir . '/orignal/' . $this->_arrData['current_picture']);
        $upload->removeFile($upload_dir . '/img100x100/' . $this->_arrData['current_picture']);
        $upload->removeFile($upload_dir . '/img450x450/' . $this->_arrData['current_picture']);
      }
    } else {
      if ($this->_arrData['action'] == 'edit') {
        $fileName = $this->_arrData['current_picture'];
      }
    }
    return $fileName;
  }
}
